package com.quik.web.token.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.quik.common.Constants;
import com.quik.dto.PaginationDto;
import com.quik.entity.SysAccount;
import com.quik.entity.DtaTokenType;
import com.quik.exception.ErrorCode;
import com.quik.web.product.service.ProductService;
import com.quik.web.token.service.TokenService;

/**
 * 功能说明：
 *      手牌类型管理业务控制类
 *      
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/tokenType")
public class TokenTypeController {
	private static Logger log = (Logger)Logger.getLogger(TokenTypeController.class);
	
	@Autowired
	private TokenService tokenService;
	
	@Autowired
	private ProductService productService;
	
	/**
	 * 进入手牌类型管理界面
	 * @return
	 */
	@RequestMapping(value = "tokenTypeList", method = RequestMethod.GET)
	public String tokenTypeList(){
		return "/token/tokenType/tokenTypeList";
	}
	
	/**
	 * 查询手牌类型
	 * @param page
	 * @param rows
	 * @param tokenTypeName
	 * @param productName
	 * @param productNo
	 * @return
	 */
	@RequestMapping(value = "queryTokenType", method = RequestMethod.POST)
	@ResponseBody
	public PaginationDto queryTokenType(Integer page, Integer rows, String tokenTypeName, String productName, String productNo){
		PaginationDto dto = new PaginationDto(0, null);
		
		try{
			int intPage = (page == null || page == 0) ? 1:page;  //当前页
		    int pageSize = (rows == null || rows == 0) ? Constants.TREE_PAGE_SIZE : rows;  //每页显示的记录
		    
		    int totalCount = tokenService.findTokenTypeCount(tokenTypeName, productName, productNo);
		    List list = tokenService.findTokenType(tokenTypeName, productName, productNo, page, rows);
		    
		    dto.setTotal(totalCount);
		    dto.setRows(list);
		    
		}catch(Exception e){
			e.printStackTrace();
			log.error("TokenTypeController queryTokenType error: " + e.getMessage());
		}
		
		return dto;
	}
	
	/**
	 * 检查手牌类型名称是否重复
	 * @param tokenTypeName
	 * @return
	 */
	@RequestMapping(value = "checkTokeTypeName", method = RequestMethod.POST)
	public ResponseEntity<String> checkTokenTypeName(String tokenTypeName){
		boolean flag = false;
		
		try{
			flag = tokenService.checkTokenTypeName(tokenTypeName);
		}catch(Exception e){
			e.printStackTrace();
			log.error("TokenTypeController checkTokenTypeName error: " + e.getMessage());
		}
		
		return new ResponseEntity<String>(String.valueOf(flag), HttpStatus.OK);
	}
	
	/**
	 * 进入新增手牌初始界面 
	 * @return
	 */
	@RequestMapping(value = "initAddTokenType")
	public ModelAndView initAddTokenType(HttpServletRequest request){
		ModelAndView mav = new ModelAndView("/token/tokenType/addTokenType");
		String result = (String)request.getAttribute("result");
		
		try{
			List categoryList = productService.findCategory();
			
			if(result != null && result.length() > 0)
				mav.addObject("result", result);
			mav.addObject("categoryList", categoryList);
		}catch(Exception e){
			e.printStackTrace();
			log.error("TokenTypeController initAddTokenType error: " + e.getMessage());
		}
		return mav;
	}
	
	/**
	 * 新增手牌类型
	 * @param tokenType
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addTokenType", method = RequestMethod.POST)
	public ModelAndView addTokenType(DtaTokenType tokenType, HttpServletRequest request){
		ModelAndView mav = new ModelAndView("/token/tokenType/addTokenType");
		SysAccount account = (SysAccount)request.getSession().getAttribute(Constants.SESSION_ACCOUNT);
		String result = ErrorCode.SYSTEM_ERROR;
		
		try{
			tokenType.setCrt_user(account.getStaff_no());
			tokenType.setCrt_date(new java.util.Date(System.currentTimeMillis()));
			
			tokenService.addTokenType(tokenType);
			result = ErrorCode.RESULT_ADD_TOKEN_TYPE_SUCCESS;
			
		}catch(Exception e){
			e.printStackTrace();
			log.error("TokenTypeController addTokenType error: " + e.getMessage());
		}
		
		mav.addObject("result", result);
		mav.setViewName("forward:/tokenType/initAddTokenType");
		return mav;
	}
}
